package com.situ.day6;

import org.junit.Test;

import java.util.Arrays;

public class StringDemo {
    @Test
    public void test1() {
        //字符串常量
        int num = 4;
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "abc";
        //==:比较的是变量指向的对象的地址值
        System.out.println(str1 == str2);//false
        System.out.println(str1 == str3);//true
        System.out.println(str2 == str3);//false
        //equals:比较字符串的内容是不是相等
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//true
        System.out.println(str2.equals(str3));//true
    }

    @Test
    public void test11() {
        String str = "java AndroidA";
        System.out.println(str.length());//13
        char[] chars = str.toCharArray();
        char ch = str.charAt(5);
        System.out.println(ch);//A
        System.out.println(str.indexOf('A'));//5
        System.out.println(str.indexOf('X'));//-1
        System.out.println(str.indexOf('A', 6));//12
        System.out.println(str.indexOf("And"));//5
        System.out.println(str.lastIndexOf('A'));//12
    }

    @Test
    public void test2() {
        String str = "java AndroidA";
        System.out.println(str.contains("And"));//true
        System.out.println(str.startsWith("java"));//true
        System.out.println(str.startsWith("And", 5));//true
        System.out.println(str.endsWith("idA"));//true
        System.out.println(str.isEmpty());//false
        //char ch = '';
        // ""空字符串，是个字符串常量，是对象
        String str1 = "";
        System.out.println("".isEmpty());//true
        System.out.println("abc".isEmpty());//false
    }

    @Test
    public void test34() {
        //String str = "java And";
        char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
        String str = new String(array);
        System.out.println(str);
        char[] chars = str.toCharArray();
        //增强的for循环，不需要操作下标
        for (char ch : chars) {
            System.out.println(ch);
        }
        System.out.println(chars);
        int[] arr = {2, 34, 56};
        System.out.println(arr);

        //Arrays 操作数组工具类  ArrayUtil
        System.out.println(Arrays.toString(chars));
        System.out.println(Arrays.toString(arr));
    }

    @Test
    public void test12() {
        String str = "java AndroidA";
        String newStr = str.replace('A', 'B');
        System.out.println(newStr);
    }

    @Test
    public void test13() {
        String str = "java Android Python";
        String[] array = str.split(" ");
        System.out.println(Arrays.toString(array));
    }

    @Test
    public void test14() {
        String str = "java AndroidA";
        System.out.println(str.substring(5));
        //beginIndex – the beginning index, inclusive.
        //endIndex – the ending index, exclusive
        System.out.println(str.substring(5, 8));

    }

    @Test
    public void test15() {
        String str = "   java AndroidA    ";
        System.out.println(str);
        System.out.println(str.trim());
    }

    @Test
    public void test341() {
        String str1 = "abc";
        String str2 = "def";
        String str3 = str1 + str2;
        System.out.println(str3);

        String str = new String("abc");
        StringBuffer stringBuffer = new StringBuffer("abc");
        StringBuilder stringBuilder = new StringBuilder("abc");
    }

    @Test
    public void test345() {
        String str1 = "python";
        String str2 = "C++";
        String str3 = "UI";
        StringBuilder builder = new StringBuilder("");
        builder.append(str1);
        builder.append(str2);
        builder.append(str3);
        System.out.println(builder);

        StringBuffer buffer1 = new StringBuffer("");
        buffer1.append(str1);
        buffer1.append(str2);
        buffer1.append(str3);
        System.out.println(buffer1);

        StringBuffer buffer2 = new StringBuffer("");
        buffer2.append(str1).append(str2).append(str3);
    }

    @Test
    public void test45() {
        System.out.println(Integer.MAX_VALUE);

        //自动拆箱和装箱
        //Integer integer = new Integer(4);
        //自动装箱操作，等价于上面操作
        Integer integer = 4;
        int i = integer.intValue();

        int num = 5;
        num = num + 3;

        //integer.intValue()  自动拆箱
        // 4+3
        //new Integer(7) 自动装箱
        integer = integer + 3;
        System.out.println(integer);

    }

    @Test
    public void test3415() {
//        包装数据类型和字符串之间的转换
//        1、把基本数据类型转换成字符串
//         (1) 基本数据类型+"" :   String str = 12 + ""; // "12"
//        (2) 基本数据类型的包装类.toString()
//        String str1 = Integer.toString(3);
//        String str2 = Double.toString(3.14);
        String str = 12 + "";// ""
        String str1 = Integer.toString(12);
        String str2 = Double.toString(3.14);//"3.14"

//        2、把字符串转换成基本数据类型（从网页的输入框中得到的都是字符串）
//        (1) Integer.parseInt(String str);  // "23"  "3.14"
//        Double.parseDouble(String str);
//        Boolean.parseBoolean(String str);
//        XXXX.parseXXXX(String str);
//        (2) new对象： Integer integer = new Integer(String str);
//        Integer interger = new Integer("4");
//        int i = integer.intValue();
        int i = Integer.parseInt("23");//23
        double d = Double.parseDouble("3.14");
        boolean bool = Boolean.parseBoolean("true");

        Integer integer = new Integer("24");
        int i2 = integer.intValue();

    }

}
